//
//  LoveSongViewController.m
//  NhomThuy02
//
//  Created by Huynh Tri Dung on 2/5/15.
//  Copyright (c) 2015 LamQuoc. All rights reserved.
//

#import "LoveSongViewController.h"
#import "FavoriteTableViewCell.h"
#import "MyButton.h"
#import "SongDetailViewController.h"
@interface LoveSongViewController ()<UISearchControllerDelegate,UISearchDisplayDelegate>
@property NSIndexPath *mIndexPath;
@property (strong,nonatomic) NSMutableArray* loveSongList;
@property (nonatomic,strong)NSMutableArray *searchArray;
@property (nonatomic,strong)NSString *searchString;
@end

@implementation LoveSongViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.TableView.delegate=self;
    self.TableView.dataSource=self;
    self.loveSongList = [[ManagerData defaultManager] getLoveSongList];
    UILongPressGestureRecognizer* _longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self.overlay action:@selector(longPressDetected:)];
    [self.btnMenu addGestureRecognizer:_longPressRecognizer];
    self.searchString =@"";
}
- (void)viewWillAppear:(BOOL)animated{
    [self.TableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (![self.searchString isEqualToString:@""]) {
        return self.searchArray.count;
    }
    return self.loveSongList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 82;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString* myIdentifier = @"Cell_Identifier";
    FavoriteTableViewCell  * cell = nil;
    cell = [self.TableView dequeueReusableCellWithIdentifier:myIdentifier];
    if (cell==nil) {
        [self.TableView registerClass:[FavoriteTableViewCell class] forCellReuseIdentifier:myIdentifier];
        cell=[self.TableView dequeueReusableCellWithIdentifier:myIdentifier];
        
    }
    NSDictionary* loveSongDic;
    if (![self.searchString isEqualToString:@""]) {
        loveSongDic = [self.searchArray objectAtIndex:indexPath.row];
    }else{
        loveSongDic = [self.loveSongList objectAtIndex:indexPath.row];
    }
    cell.SondId.text=[loveSongDic objectForKey:@"song-code"];
    cell.SongName.text =[[loveSongDic objectForKey:@"song-name"]capitalizedString];
    cell.Lyrics.text =[loveSongDic objectForKey:@"lyrics"];
    cell.btnLike.btnData = indexPath;
    [cell.btnLike addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside];
    self.mIndexPath = indexPath;
    [cell.SondId setBackgroundColor:[UIColor greenLightColor]];
    return cell;
}

- (IBAction)onclick:(MyButton*)sender
{
    NSLog(@"click");
    self.mIndexPath = sender.btnData;
    NSDictionary* loveSongDic;
    if (![self.searchString isEqualToString:@""]) {
        loveSongDic = [self.searchArray objectAtIndex:self.mIndexPath.row];
    }else{
        loveSongDic = [self.loveSongList objectAtIndex:self.mIndexPath.row];
    }
    NSString *songName = [loveSongDic objectForKey:@"song-name"];
    NSString *alertContent = [NSString stringWithFormat:@"Bạn có đồng ý bỏ thích bài hát \n%@",songName];
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Bỏ thích bài hát" message:alertContent delegate:self cancelButtonTitle:@"Huỷ" otherButtonTitles:@"Đồng ý",nil];
    [alertView show];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSDictionary* loveSongDic;
    if (![self.searchString isEqualToString:@""]) {
        loveSongDic = [self.searchArray objectAtIndex:self.mIndexPath.row];
    }else{
        loveSongDic = [self.loveSongList objectAtIndex:self.mIndexPath.row];
    }
    if(buttonIndex == 0)
    {
        // do nothing
    }
    else
    {
        if (![self.searchString isEqualToString:@""]) {
            [self.searchArray removeObject:loveSongDic];
            
            [self.searchDisplayController.searchResultsTableView reloadData];
        }
    
        [self.loveSongList removeObject:loveSongDic];
        [self.TableView reloadData];
        NSLog(@"index path = %ld",(long)self.mIndexPath.row);
        //[ManagerData defaultManager].loveSongList = self.loveSongList;
        
        
        
//        [self.TableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.mIndexPath] withRowAnimation:UITableViewRowAnimationNone];
        [[ManagerData defaultManager]removeLoveSongWithDictionaryOfSong:loveSongDic];
    }
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath* indexPath;
    
    NSDictionary* dictionary;
    if (![self.searchString isEqualToString:@""]) {
        indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
        dictionary = [self.searchArray objectAtIndex:indexPath.row];
      
        
    }else{
        indexPath = [self.TableView indexPathForSelectedRow];
        dictionary = [self.loveSongList objectAtIndex:indexPath.row];
    }
    ((SongDetailViewController*)segue.destinationViewController).songData = dictionary;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    NSPredicate *pre2 = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        NSDictionary *song =evaluatedObject;
        NSString *name =[song objectForKey:@"song-name"];
        if([[name lowercaseString] containsString:[searchString lowercaseString]]) {
            return YES;
        } else {
            return NO;
        }
    }];
    NSArray *s =[self.loveSongList filteredArrayUsingPredicate:pre2];
    self.searchArray =[NSMutableArray arrayWithArray:s];
    [self.searchDisplayController.searchResultsTableView reloadData];
    self.searchString= searchString;
    return YES;
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller{
    self.searchString = @"";
    [self.TableView reloadData];
}
@end
